"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
var PIXI = require("@tbminiapp/pixi-miniprogram-engine");
var windowAlias = PIXI.miniprogram.windowAlias;
var requestAnimationFrame = windowAlias.requestAnimationFrame || windowAlias.webkitRequestAnimationFrame;
/**
 * @module utils
 */
/**
 * 计时器对象
 */
class Timer extends PIXI.utils.EventEmitter {
    /**
     * 创建一个计时器对象
     *
     * ```
     * const timer = new Timer(1000);
     * timer.setOnTimer(({ repeatTimes, isRunning, timer }) => {
     *
     * });
     * timer.start(); // 开始计时
     * timer.stop(); // 停止计时
     * timer.reset(); // 重置计时器
     * timer.destroy(); // 销毁计时器
     * ```
     *
     * @param tickerTime 计时器跳动间隔(单位：毫秒)
     * @param [repeatCount] 计时器跳动次数上限。默认不限
     */
    constructor(tickerTime, repeatCount) {
        super();
        if (repeatCount === void 0) { repeatCount = 0; }
        // var _this = _super.call(this) || this;
        var _this = this;
        /**
         * 计时器是否正在运行
         */
        _this._isRunning = false;
        _this._currentRepeat = 0;
        _this._tickerTime = tickerTime;
        _this._repeatCount = repeatCount;
        _this._isRunning = false;
        return _this;
    }
   
    /**
     * 设置跳动回调
     * @param fun 回调函数
     */
    setOnTimer(fun) {
      this._onTimer = fun;
    };
    /**
     * 启动计时器
     */
    start() {
        var _this = this;
        this._isRunning = true;
        this._lastTime = Date.now();
        requestAnimationFrame(function () {
            _this.tick();
        });
    };
    /**
     * 跳动
     */
    tick() {
        var _this = this;
        this._currentTime = Date.now();
        if (this._isRunning) {
            if (this._currentTime - this._lastTime >= this._tickerTime) {
                this._lastTime = this._currentTime;
                this._currentRepeat++;
                if (typeof this._onTimer === 'function') {
                    this._onTimer.call(null, {
                        repeatTimes: this._currentRepeat,
                        isRunning: this._isRunning,
                        timer: this
                    });
                }
                if (this._repeatCount > this._currentRepeat || this._repeatCount === 0) {
                    requestAnimationFrame(function () {
                        _this.tick();
                    });
                }
                else {
                    this.stop();
                }
            }
            else {
                requestAnimationFrame(function () {
                    _this.tick();
                });
            }
        }
    };
    /**
     * 销毁计时器
     */
    destroy() {
        this.clear();
    };
    /**
     * 暂停计时器
     */
    stop() {
        this._isRunning = false;
    };
    /**
     * 清除计时器数据并停止
     */
    clear() {
        this.stop();
        this._onTimer = undefined;
        this.removeAllListeners();
        this._currentRepeat = 0;
    };
    /**
     * 计时器数据清零，但不主动停止
     */
    reset = function () {
        this._currentRepeat = 0;
    };
}

export default Timer
